﻿@model IEnumerable<EdexApiClient.Invoices>

@{
    ViewBag.Title = "Документы";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<header class="jumbotron subhead" id="overview">
    <h1></h1>
    <p class="lead">Исходящие накладные</p>
</header>

<form>
    <div class="input-append">
        <input type="text" class="input-xxlarge" name="search" value="@Request["search"]" />
        <button class="btn" type="submit"><i class="icon-filter"></i> Фильтр</button>
    </div>
</form>

<table data-name="INVOICES" class="table table-striped">
    <thead>
        <tr>
            <th>Получатель</th>
            <th>Склад</th>
            <th>Дата документа</th>
            <th>№ документа</th>
            <th>Сумма</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var record in Model) {
        <tr data-index="@record.GUID_RECORD">
            <td>@(record.CUSTOMER != null ? record.CUSTOMER.COMPANY_NAME : String.Empty)</td>
            <td>@(record.LOCATION != null ? record.LOCATION.LOCATION_NAME : String.Empty)</td>
            <td>@record.DOC_DATE.ToShortDateString()</td>
            <td>@record.DOC_NUMBER</td>
            <td>@record.TOTAL_AMOUNT</td>
        </tr>
        }
    </tbody>
</table>

<p>
    <a href="#" class="btn" data-action="INVOICE-CREATE"><i class="icon-plus"></i> Создать новый документ</a>
</p>

<div id="invoice-details"></div>
<div id="invoice-line-details"></div>


<script type="text/javascript">

    $(function () {

        ///$(".table").flexigrid({ height: 'auto' });

        function LoadInvoiceForm(id) {
            $("#invoice-details").load('/document/OutgoingInvoiceEdit/' + id, function () {
                $('.modal[data-name="INVOICES"]').modal("show");
                $('#dt1').datepicker();
            });
        };

        $("table[data-name='INVOICES'] tr td[data-type!='noclick']").live("click", function () {

            var id = $(this).parent().attr("data-index");
            LoadInvoiceForm(id);
        });

        $(".btn[data-action='INVOICE-CREATE']").live("click", function () {

            $("#invoice-details").load('/document/OutgoingInvoiceNew/', function () {
                $('.modal[data-name="INVOICES"]').modal("show");
            });

        });

        $("table[data-name='INVOICE_LINE'] tr td[data-type!='noclick']").live("click", function () {

            var id = $(this).parent().attr("data-index");

            $("#invoice-line-details").load('/document/OutgoingInvoiceDetailsEdit/' + id, function () {
                $('.modal[data-name="INVOICES"]').modal("hide");
                $('.modal[data-name="INVOICE_LINE"]').modal("show");
            });
        });

        $(".btn[data-action='INVOICE_LINE-CLOSE']").live("click", function () {
            $('.modal[data-name="INVOICES"]').modal("show");
        });

        $("input[name='CUSTOMER_NAME']").live('click', function () {
            $(".company-filter-group").toggle();
        });

        $("input[name='LOCATION_NAME']").live('click', function () {
            $(".location-filter-group").toggle();
        });

        $("input[name='ITEM_NAME']").live('click', function () {
            $(".item-filter-group").toggle();
        });

        $(".btn[data-action='COMPANY-FILTER']").live("click", function () {

            var filter = $("input[name='COMPANY-FILTER']").val();
            $("select[name='COMPANY_GUID-FILTER']").load("/supplier/FindAndReturnHtmlList/" + filter);
        });

        $(".btn[data-action='COMPANY-FILTER-APPLY']").live("click", function () {
            $("input[name='CUSTOMER_GUID']").val($("select[name='COMPANY_GUID-FILTER'] option:selected").val());
            $("input[name='CUSTOMER_NAME']").val($("select[name='COMPANY_GUID-FILTER'] option:selected").text());
        });

        $(".btn[data-action='LOCATION-FILTER']").live("click", function () {

            var filter = $("input[name='LOCATION-FILTER']").val();
            $("select[name='LOCATION_GUID-FILTER']").load("/location/FindAndReturnHtmlList/" + filter);
        });

        $(".btn[data-action='LOCATION-FILTER-APPLY']").live("click", function () {
            $("input[name='LOCATION_GUID']").val($("select[name='LOCATION_GUID-FILTER'] option:selected").val());
            $("input[name='LOCATION_NAME']").val($("select[name='LOCATION_GUID-FILTER'] option:selected").text());
        });

        $(".btn[data-action='ITEM-FILTER']").live("click", function () {

            var filter = $("input[name='ITEM-FILTER']").val();
            $("select[name='ITEM_GUID-FILTER']").load("/product/FindAndReturnHtmlList/" + filter);
        });

        $(".btn[data-action='ITEM-FILTER-APPLY']").live("click", function () {
            $("input[name='ITEM_GUID']").val($("select[name='ITEM_GUID-FILTER'] option:selected").val());
            $("input[name='ITEM_NAME']").val($("select[name='ITEM_GUID-FILTER'] option:selected").text());
        });

        $(".btn[data-action='INVOICE_LINE-SAVE']").live("click", function () {

            var invoiceId = $("form[name='INVOICES'] *[name='INVOICE_GUID']").val();

            $.ajax({
                type: 'POST',
                url: '/document/SaveOutgoingInvoiceLine/',
                data: $("form[name='INVOICE_LINE']").serialize(),
                success: function () {
                    $('.modal[data-name="INVOICE_LINE"]').modal("hide");

                    LoadInvoiceForm(invoiceId);
                }
            });

        });

        $(".btn[data-action='INVOICE_LINE-CREATE']").live("click", function () {

            var invoiceId = $("form[name='INVOICES'] *[name='GUID_RECORD']").val();

            $("#invoice-line-details").load('/document/OutgoingInvoiceDetailsNew/' + invoiceId, function () {
                $('.modal[data-name="INVOICES"]').modal("hide");
                $('.modal[data-name="INVOICE_LINE"]').modal("show");
            });

        });

        $(".btn[data-action='INVOICE_LINE-DELETE']").live("click", function () {

            var id = $("form[name='INVOICE_LINE'] *[name='GUID_RECORD']").val();
            var invoiceId = $("form[name='INVOICES'] *[name='GUID_RECORD']").val();

            $.ajax({
                type: 'POST',
                url: '/document/DeleteOutgoingInvoiceLine/' + id,
                success: function () {
                    $('.modal[data-name="INVOICE_LINE"]').modal("hide");
                    LoadInvoiceForm(invoiceId);
                }
            });

        });

        $(".btn[data-action='INVOICE-DELETE']").live("click", function () {

            var id = $("form[name='INVOICES'] *[name='GUID_RECORD']").val();

            $.ajax({
                type: 'POST',
                url: '/document/DeleteOutgoingInvoice/' + id,
                success: function () {
                    $('.modal[data-name="INVOICES"]').modal("hide");
                }
            });

        });


        $(".btn[data-action='INVOICE-SAVE']").live("click", function () {

            $.ajax({
                type: 'POST',
                url: '/document/SaveOutgoingInvoice/',
                data: $("form[name='INVOICES']").serialize(),
                success: function () {
                    $('.modal[data-name="INVOICES"]').modal("hide");
                }
            });

        });

    });
</script>
